package com.sqx.modules.userCertification.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.app.utils.VerifyIdCardUtils;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.sign.dao.GoldRecordsDao;
import com.sqx.modules.sign.entity.GoldRecords;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.task.dao.TbDailyTasksDao;
import com.sqx.modules.task.dao.TbUserTaskDao;
import com.sqx.modules.task.entity.TbDailyTasks;
import com.sqx.modules.task.entity.TbUserTask;
import com.sqx.modules.userCertification.dao.UserCertificationDao;
import com.sqx.modules.userCertification.entity.UserCertification;
import com.sqx.modules.userCertification.service.UserCertificationService;
import com.sqx.modules.utils.IdentityCardUtil;
import com.sqx.modules.utils.SenInfoCheckUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 实名认证 服务实现类
 *
 * @author wuChang
 * @since 2024-03-22
 */
@Service
public class UserCertificationServiceImp extends ServiceImpl<UserCertificationDao, UserCertification> implements UserCertificationService {
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;

    @Autowired
    private GoldRecordsDao goldRecordsDao;

    @Autowired
    private TbUserTaskDao tbUserTaskDao;

    @Autowired
    private TbDailyTasksDao tbDailyTasksDao;

    @Override
    public Result saveUserCertification(UserCertification userCertification) {
        UserEntity userEntity = userService.getById(userCertification.getUserId());
        if (userEntity == null) {
            return Result.error("用户信息已失效,请重新登录");
        }
        if (userCertification.getId() != null) {
            UserCertification certificate = baseMapper.selectById(userCertification.getId());
            if (certificate.getIsSubmit() != null && certificate.getIsSubmit() == 1) {
                return Result.error("您不可以再次提交");
            }
            String value = commonInfoService.findOne(303).getValue();
            if ("1".equals(value)) {
                userCertification.setStatus(0);
                userEntity.setSex(IdentityCardUtil.getSex(userCertification.getIdNumber()));
                userEntity.setAge(IdentityCardUtil.getAge(userCertification.getIdNumber()));
                userEntity.setIsAuth(1);
                userService.updateById(userEntity);
            } else {
                String result = VerifyIdCardUtils.verifyIdCard(userCertification.getIdNumber(), userCertification.getRealName());
                if ("ok".equals(result)) {
                    userCertification.setAuditTime(LocalDateTime.now());
                    userCertification.setStatus(1);
                    userCertification.setIsSubmit(0);
                    if (StringUtils.isNotBlank(userEntity.getOpenId())) {
//                        List<String> msgList = new ArrayList<>();
//                        msgList.add("实名认证");
//                        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//                        msgList.add(fmt.format(LocalDateTime.now()));
//                        msgList.add("审核已通过");
//                        msgList.add("审核已通过,如有问题请联系管理员");
//                        CommonInfo one = commonInfoService.findOne(292);
//                        msgList.add(fmt.format(LocalDateTime.now()));
//                        if (one != null) {
//                            SenInfoCheckUtil.sendMsg(userEntity.getOpenId(), one.getValue(), msgList, 1);
//                        }
                    }
                    MessageInfo messageInfo = new MessageInfo();
                    messageInfo.setContent("您的实名认证审核已通过");
                    messageInfo.setTitle("审核通知");
                    messageInfo.setState(String.valueOf(6));
                    messageInfo.setUserName(userEntity.getUserName());
                    messageInfo.setUserId(String.valueOf(userEntity.getUserId()));
                    messageInfo.setCreateAt(DateUtils.format(new Date()));
                    messageInfo.setIsSee("0");
                    messageService.saveBody(messageInfo);

                    if (StringUtils.isNotEmpty(userEntity.getClientid())) {
                        userService.pushToSingle("审核通知", "您的实名认证审核已通过", userEntity.getClientid());
                    }
                    // 新增实名认证通过后增加金币的记录
                    if (userCertification.getTaskId() != null) {
                        TbUserTask tbUserTask = tbUserTaskDao.selectByUserId(userCertification.getTaskId(), userCertification.getUserId());
                        if (tbUserTask == null) {
                            TbUserTask tbUserTask1 = new TbUserTask();
                            tbUserTask1.setTaskId(userCertification.getTaskId());
                            tbUserTask1.setUserId(userCertification.getUserId());
                            tbUserTask1.setState(1);
                            tbUserTask1.setCreateTime(new Date());
                            tbUserTask1.setRewardClaimed(0);
                            tbUserTaskDao.insert(tbUserTask1);
                        }
//                        GoldRecords goldRecords = new GoldRecords();
//                        goldRecords.setUserId(userCertification.getUserId());
//                        goldRecords.setGoldChange(1);
//                        goldRecords.setRemark("实名认证奖励");
//                        goldRecords.setCreateTime(new Date());
//                        goldRecords.setRelatedId(userCertification.getId());
//                        TbDailyTasks tbDailyTasks = tbDailyTasksDao.selectById(userCertification.getTaskId());
//                        goldRecords.setGoldCoins(tbDailyTasks.getSpecies());
//                        goldRecordsDao.insert(goldRecords);
//                        userEntity.setTotalGold(userEntity.getTotalGold() + tbDailyTasks.getSpecies());
                        userEntity.setSex(IdentityCardUtil.getSex(userCertification.getIdNumber()));
                        userEntity.setAge(IdentityCardUtil.getAge(userCertification.getIdNumber()));
                        userEntity.setIsAuth(1);
                        userService.updateById(userEntity);
                    }
                    userEntity.setSex(IdentityCardUtil.getSex(userCertification.getIdNumber()));
                    userEntity.setAge(IdentityCardUtil.getAge(userCertification.getIdNumber()));
                    userEntity.setIsAuth(1);
                    userEntity.setStatus1(1);
                    userService.updateById(userEntity);
                } else {
                    return Result.error("您填写的信息有误，请检查后重试！");
                }
            }
            return Result.upStatus(baseMapper.updateById(userCertification));
        } else {
            int count = baseMapper.selectCount(new QueryWrapper<UserCertification>()
                    .eq("id_number",userCertification.getIdNumber()));
            if (count >= 1) {
                return Result.error("您已上传实名认证，请勿重复提交！");
            }
            int counts = baseMapper.selectCount(new QueryWrapper<UserCertification>()
                    .eq("user_id",userCertification.getUserId()));
            if (counts >= 1) {
                return Result.error("您已上传实名认证，请勿重复提交！");
            }
            UserCertification oldUserCertification = baseMapper.selectOne(new QueryWrapper<UserCertification>()
                    .eq("id_number", userCertification.getIdNumber()));
            if (oldUserCertification != null) {
                return Result.error("您已上传实名认证，请查看认证信息状态");
            } else {
                String value = commonInfoService.findOne(303).getValue();
                if ("1".equals(value)) {
                    userCertification.setStatus(0);
                    userCertification.setIsSubmit(0);
                    userEntity.setSex(IdentityCardUtil.getSex(userCertification.getIdNumber()));
                    userEntity.setAge(IdentityCardUtil.getAge(userCertification.getIdNumber()));
                    userEntity.setIsAuth(1);
                    userService.updateById(userEntity);
                } else {
                    String result = VerifyIdCardUtils.verifyIdCard(userCertification.getIdNumber(), userCertification.getRealName());
                    if ("ok".equals(result)) {
                        userCertification.setAuditTime(LocalDateTime.now());
                        userCertification.setStatus(1);
                        userCertification.setIsSubmit(0);
//                        if (StringUtils.isNotBlank(userEntity.getOpenId())) {
//                            List<String> msgList = new ArrayList<>();
//                            msgList.add("实名认证");
//                            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//                            msgList.add(fmt.format(LocalDateTime.now()));
//                            msgList.add("审核已通过");
//                            msgList.add("审核已通过,如有问题请联系管理员");
//                            CommonInfo one = commonInfoService.findOne(292);
//                            msgList.add(fmt.format(LocalDateTime.now()));
//                            if (one != null) {
//                                SenInfoCheckUtil.sendMsg(userEntity.getOpenId(), one.getValue(), msgList, 1);
//                            }
//                        }
                        MessageInfo messageInfo = new MessageInfo();
                        messageInfo.setContent("您的实名认证审核已通过");
                        messageInfo.setTitle("审核通知");
                        messageInfo.setState(String.valueOf(6));
                        messageInfo.setUserName(userEntity.getUserName());
                        messageInfo.setUserId(String.valueOf(userEntity.getUserId()));
                        messageInfo.setCreateAt(DateUtils.format(new Date()));
                        messageInfo.setIsSee("0");
                        messageService.saveBody(messageInfo);

                        if (StringUtils.isNotEmpty(userEntity.getClientid())) {
                            userService.pushToSingle("审核通知", "您的实名认证审核已通过", userEntity.getClientid());
                        }
                        // 新增实名认证通过后增加金币的记录
                        if (userCertification.getTaskId() != null) {
                            TbUserTask tbUserTask = tbUserTaskDao.selectByUserId(userCertification.getTaskId(), userCertification.getUserId());
                            if (tbUserTask == null) {
                                TbUserTask tbUserTask1 = new TbUserTask();
                                tbUserTask1.setTaskId(userCertification.getTaskId());
                                tbUserTask1.setUserId(userCertification.getUserId());
                                tbUserTask1.setState(1);
                                tbUserTask1.setCreateTime(new Date());
                                tbUserTask1.setRewardClaimed(0);
                                tbUserTaskDao.insert(tbUserTask1);
                            }
//                        GoldRecords goldRecords = new GoldRecords();
//                        goldRecords.setUserId(userCertification.getUserId());
//                        goldRecords.setGoldChange(1);
//                        goldRecords.setRemark("实名认证奖励");
//                        goldRecords.setCreateTime(new Date());
//                        goldRecords.setRelatedId(userCertification.getId());
//                        TbDailyTasks tbDailyTasks = tbDailyTasksDao.selectById(userCertification.getTaskId());
//                        goldRecords.setGoldCoins(tbDailyTasks.getSpecies());
//                        goldRecordsDao.insert(goldRecords);
//                        userEntity.setTotalGold(userEntity.getTotalGold() + tbDailyTasks.getSpecies());
                            userEntity.setSex(IdentityCardUtil.getSex(userCertification.getIdNumber()));
                            userEntity.setAge(IdentityCardUtil.getAge(userCertification.getIdNumber()));
                            userEntity.setIsAuth(1);
                            userService.updateById(userEntity);
                        }
                        userEntity.setSex(IdentityCardUtil.getSex(userCertification.getIdNumber()));
                        userEntity.setAge(IdentityCardUtil.getAge(userCertification.getIdNumber()));
                        userEntity.setIsAuth(1);
                        userEntity.setStatus1(1);
                        userService.updateById(userEntity);
                    } else {
                        return Result.error("您填写的信息有误，请检查后重试！");
                    }
                }
                userCertification.setCreateTime(LocalDateTime.now());
                return Result.upStatus(baseMapper.insert(userCertification));
            }
        }
    }

    @Override
    public IPage<UserCertification> getUserCertificationList(Integer page, Integer limit, UserCertification userCertification) {
        Page<UserCertification> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.getUserCertificationList(pages, userCertification);
    }

    @Override
    public Result deleteUserCertificationById(Long id) {
        UserCertification userCertification = baseMapper.selectById(id);
        if (userCertification == null) {
            return Result.error("所选实名认证不存在");
        } else {
            UserEntity userEntity = userService.getById(userCertification.getUserId());
            if (userEntity != null) {
                userEntity.setIsAuth(0);
                userService.updateById(userEntity);
            }
        }
        return Result.upStatus(baseMapper.deleteById(id));

    }

    @Override
    public Result authCertification(Long id, Integer status, String remark, Long taskId, Integer isSubmit) {
        UserCertification userCertification = baseMapper.selectById(id);
        if (userCertification == null) {
            return Result.error("实名认证不存在");
        }
        if (status == 1) {
            UserEntity userEntity = userService.selectUserById(userCertification.getUserId());
//            userEntity.setIsAuth(1);
            userEntity.setStatus1(1);
            userService.updateById(userEntity);
            userCertification.setStatus(1);
            userCertification.setRemark(" ");
        } else {
            userCertification.setStatus(2);
            userCertification.setRemark(remark);
            UserEntity userEntity = userService.selectUserById(userCertification.getUserId());
            if (userEntity == null) {
                return Result.error("用户不存在");
            }
            userEntity.setIsAuth(2);
            userEntity.setStatus1(2);
            userService.updateById(userEntity);
            userCertification.setIsSubmit(isSubmit);
            setUserBaseInfo(userCertification.getIdNumber(), userCertification.getUserId());
        }
        if (userCertification.getTaskId() != null) {
            TbUserTask tbUserTask = tbUserTaskDao.selectByUserId(userCertification.getTaskId(), userCertification.getUserId());
            if (tbUserTask == null) {
                TbUserTask tbUserTask1 = new TbUserTask();
                tbUserTask1.setTaskId(userCertification.getTaskId());
                tbUserTask1.setUserId(userCertification.getUserId());
                tbUserTask1.setState(1);
                tbUserTask1.setCreateTime(new Date());
                tbUserTask1.setRewardClaimed(0);
                tbUserTaskDao.insert(tbUserTask1);
            }
        }
        // 新增实名认证通过后增加金币的记录
//        GoldRecords goldRecords = new GoldRecords();
//        goldRecords.setUserId(userCertification.getUserId());
//        goldRecords.setGoldChange(1);
//        goldRecords.setRemark("实名认证奖励");
//        goldRecords.setCreateTime(new Date());
//        goldRecords.setRelatedId(userCertification.getId());
//        goldRecords.setGoldCoins(Integer.valueOf(commonInfoService.findOne(509).getValue()));
//        goldRecordsDao.insert(goldRecords);
//        UserEntity userEntity = userService.getById(userCertification.getUserId());
//        userEntity.setTotalGold(userEntity.getTotalGold() + Integer.valueOf(commonInfoService.findOne(509).getValue()));
//        userService.updateById(userEntity);
        userCertification.setAuditTime(LocalDateTime.now());
        return Result.upStatus(baseMapper.updateById(userCertification));
    }

    public void setUserBaseInfo(String idNumber, Long userId) {
        UserEntity userEntity = userService.getById(userId);
        //年龄
        int genderNum = Integer.parseInt(idNumber.substring(idNumber.length() - 2, idNumber.length() - 1));
        if (genderNum % 2 == 0) {
            //女
            userEntity.setSex(2);
        } else {
            //男
            userEntity.setSex(1);
        }
        userService.updateById(userEntity);
    }

}
